//
//  Scanner+UIColorTestCase.swift
//  MKCodableTests
//
//  Created by 苏尚进 on 2020/5/21.
//  Copyright © 2020 moon. All rights reserved.
//

import Foundation
import MKCodable
import XCTest
class ScannerXCTest :XCTestCase {
//    func testT(){
//        Scanner.init(string: "#ff0033").scanColor(representation: .hex)
//    }
    func _testColorSuccess(string:String,color:UIColor,representation: NameSpace<Scanner>.ColorRepresentation){
        _testColor(string: string, color: color, auto: true, representation: nil, testFail: false)
        _testColor(string: string, color: color, auto: false, representation: representation, testFail: false)
    }
    func _testColorFail(string:String,representation: NameSpace<Scanner>.ColorRepresentation){
        _testColor(string: string, color: nil, auto: true, representation: nil, testFail: true)
        _testColor(string: string, color: nil, auto: false, representation: representation, testFail: true)
    }
    func _testColor(string:String,color value:UIColor?,auto:Bool,representation: NameSpace<Scanner>.ColorRepresentation?,testFail:Bool){
        var color:UIColor?
        if auto {
            color = Scanner.init(string: string).mk.scanColor()
        } else {
            color = Scanner.init(string: string).mk.scanColor(representation: representation!)
        }
        if testFail {
            XCTAssert(!_colorEq(color, value) || color == nil, "convert \(string) should failed")
        }else{
            XCTAssert(_colorEq(color, value), "convert \(string) should success:\(String(describing: color)),\(String(describing: value))")
        }
    }
    func _colorEq(_ c1:UIColor?,_ c2:UIColor?) -> Bool {
        if c1 == nil && c2 == nil {
            return true
        }
        if c1 == nil && c2 != nil || c1 != nil && c2 == nil {
            return false
        }
    
        let (r1,g1,b1,a1) = c1!.mk.rgba
        let (r2,g2,b2,a2) = c2!.mk.rgba
        if r1 == r2 && g1 == g2 && b1 == b2 && a1 == a2 {
            return true
        }else{
            let (h1,s1,b1,a1) = c1!.mk.hsba
            let (h2,s2,b2,a2) = c2!.mk.hsba
            return h1 == h2 && s1 == s2 && b1 == b2 && a1 == a2
        }
    }
    func rgba(_ r:Float,_ g:Float,_ b:Float,_ a:Float=1.0) -> UIColor {
        return UIColor.init(red: CGFloat(r), green: CGFloat(g), blue: CGFloat(b), alpha: CGFloat(a))
    }
    func hsla(_ h:Float,_ s:Float,_ b:Float,_ a:Float=1.0) -> UIColor{
        return UIColor.init(hue: CGFloat(h), saturation: CGFloat(s), brightness: CGFloat(b), alpha: CGFloat(a))
    }
    
    func testScanerName1(){ _testColorSuccess(string: "black", color: .black, representation: .name) }
    func testScanerHex1(){ _testColorSuccess(string: "#ff0033", color: rgba(1,0,0x33/255.0), representation: .hex) }
    func testScanerHex2(){ _testColorSuccess(string: "#FF0033", color: rgba(1,0,0x33/255.0), representation: .hex) }
    func testScanerHex3(){ _testColorSuccess(string: "#f03", color: rgba(1,0,0x33/255.0), representation: .hex) }
    func testScanerHex4(){ _testColorFail(string: "ff0033", representation: .hex) }
    func testScanerHex5(){ _testColorFail(string: "#gg0033", representation: .hex) }
    func testScanerHex6(){ _testColorFail(string: "#ff003333", representation: .hex) }
    func testScanerHex7(){ _testColorFail(string: "#f03f", representation: .hex) }
    func testScanerHex8(){ _testColorFail(string: "#ff", representation: .hex) }
    func testScannerRGB1(){ _testColorSuccess(string: "rgba(255,0,0,0.1)", color: rgba(1,0,0,0.1), representation: .rgba) }
    func testScannerRGB2(){ _testColorSuccess(string: "rgba(255,0,0,  1)", color: rgba(1,0,0,1), representation: .rgba) }
    func testScannerRGB3(){ _testColorSuccess(string: "rgb(255,0,51)", color: rgba(1,0,51/255.0), representation: .rgb) }
    func testScannerRGB4(){ _testColorSuccess(string: "rgb(255, 0, 51)", color: rgba(1,0,51/255.0), representation: .rgb) }
    func testScannerRGB5(){ _testColorSuccess(string: "rgb(100%, 0%, 20%)", color: rgba(1,0,51/255.0), representation: .rgb) }
    func testScannerRGB6(){ _testColorFail(string: "rgb(100%, 51, 220%)", representation: .rgb) }
    func testScannerRGB7(){ _testColorFail(string: "rgb(255,0,0,  1)", representation: .rgb) }
    func testScannerRGB8(){ _testColorFail(string: "rgb(256,0,0,  1)", representation: .rgb) }
    func testScannerRGB9(){ _testColorFail(string: "rgb(255,0,0,  1.1)", representation: .rgb) }
    func testScannerRGB10(){ _testColorFail(string: "rgb(255,0)", representation: .rgb) }
    func testScannerHLS1() { _testColorSuccess(string: "hsl(120,100%,25%)", color: hsla(120.0/255.0,1,0.25,1), representation: .hsl) }
    func testScannerHLS2() { _testColorSuccess(string: "hsl(120,100%,50%)", color: hsla(120.0/255.0,1,0.5,1), representation: .hsl) }
    func testScannerHLS3() { _testColorSuccess(string: "hsl(120,100%,75%)", color: hsla(120.0/255.0,1,0.75,1), representation: .hsl) }
    func testScannerHLS4() { _testColorSuccess(string: "hsla(240,100%,50%,0.05)", color: hsla(240.0/255.0,1,0.5,0.05), representation: .hsla) }
    func testScannerHLS5() { _testColorSuccess(string: "hsla(240,100%,50%, 0.4)", color: hsla(240.0/255.0,1,0.5,0.4), representation: .hsla) }
    func testScannerHLS6() { _testColorSuccess(string: "hsla(240,100%,50%, 0.7)", color: hsla(240.0/255.0,1,0.5,0.7), representation: .hsla) }
    func testScannerHLS7() { _testColorSuccess(string: "hsla(240,100%,50%,   1)", color: hsla(240.0/255.0,1,0.5,1), representation: .hsla) }
}
